package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.AffichageManager;

import beans.Commande;
import beans.User;

import affichage.AffichageSearch;

/**
 * Servlet implementation class SearchServlet
 */
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("search_field") == null
				|| request.getParameter("search_field").equals("")) {
			request.setAttribute("error", "EMPTY_SEARCH_FIELD");
			request.getRequestDispatcher("/error").forward(request, response);
		} else {
			Commande c = new Commande();
			if (request.getSession().getAttribute("panier") != null)
				c = (Commande) request.getSession().getAttribute("panier");
			AffichageSearch as = AffichageManager.buildAffichageSearch(
					(User) request.getSession().getAttribute("user"),
					request.getParameter("search_field"), c);
			request.setAttribute("affichage", as);
			request.getRequestDispatcher("/search.jsp").forward(request,
					response);
		}
	}

}
